package cn.ljy.uem.service.impl;


import cn.ljy.authority.config.security.TokenUtil;
import cn.ljy.authority.config.security.UserUtil;
import cn.ljy.authority.model.SysUser;
import cn.ljy.authority.service.SysUserTokenFrontendService;
import cn.ljy.authority.service.impl.IBaseService;
import cn.ljy.common.exception.model.MyWebException;
import cn.ljy.uem.dao.*;
import cn.ljy.uem.model.*;
import cn.ljy.uem.model.query.SchoolTeachingTaskLessonQuery;
import cn.ljy.uem.service.SchoolTeachingTaskLessonService;
import cn.ljy.uem.service.SchoolTeachingTaskStudentService;
import cn.ljy.uem.service.common.CommonSchoolService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * @description 教学任务课堂模块的业务层接口
 * @author 廖振豪
 * @date 2022-11-29
 */
@Service
@Slf4j
public class ISchoolTeachingTaskLessonService extends IBaseService<SchoolTeachingTaskLesson, SchoolTeachingTaskLessonQuery> implements SchoolTeachingTaskLessonService {

    @Autowired
    private SchoolTeachingTaskLessonMapper mapper;

    @Autowired
    private SchoolTeachingTaskMapper schoolTeachingTaskMapper;

    @Autowired
    private SchoolTeacherMapper schoolTeacherMapper;

    @Autowired
    private CommonSchoolService commonSchoolService;


    @Autowired
    private SysUserTokenFrontendService sysUserTokenFrontendService;

    @Autowired
    private SchoolTeachingTaskStudentMapper schoolTeachingTaskStudentMapper;

    @Override
    protected void beforeGet(Serializable id) {
        if(mapper.findById(id)==null){
            throw new MyWebException("查找失败：找不到指定记录");
        }
    }

    @Override
    protected void beforeUpdate(SchoolTeachingTaskLesson schoolTeachingTaskLesson) throws MyWebException {
        if(mapper.findById(schoolTeachingTaskLesson.getId())==null){
            throw new MyWebException("编辑失败：找不到指定记录");
        }
    }

    @Override
    protected void beforeSave(SchoolTeachingTaskLesson schoolTeachingTaskLesson) {

       //判断课堂是否为空
        if(schoolTeachingTaskLesson!=null){
            SchoolTeacher schoolTeacher=commonSchoolService.getLoginTeacher();
            //获取当前登录教师信息
            if(schoolTeacher!=null){
                //将教师信息存入课堂教师
                schoolTeachingTaskLesson.setCreator(schoolTeacher.getId());
                schoolTeachingTaskLesson.setSchoolTeacher(schoolTeacherMapper.findById(schoolTeacher.getId()));
                //将创建时间设置为当前时间
                schoolTeachingTaskLesson.setCreateTime(new Date());
                //将上下课状态默认设置为false
                schoolTeachingTaskLesson.setAfterClass(false);
                //校验上下课时间
//                DateTimeFormatter Dateutil = DateTimeFormatter.ofPattern("yyyy-MM-dd");
//                DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
//                String beginTime=dateformat.format(schoolTeachingTaskLesson.getBeginTime());
//                String endTime=dateformat.format(schoolTeachingTaskLesson.getEndTime());
                Date beginTime=schoolTeachingTaskLesson.getBeginTime();
                Date endTime=schoolTeachingTaskLesson.getEndTime();
                if(endTime.before(beginTime)){
                    throw new MyWebException("添加失败：下课时间在上课时间之前!");
                }


            }else {
                throw new MyWebException("获取教师信息失败!");
            }
        }else {
            throw new MyWebException("添加失败:找不到课堂信息!");
        }
    }

    @Override
    protected void beforeDelete(Serializable[] ids) {
        //查找要删除的课堂
        //创建对象 用来赋值 判断非空
//        SchoolTeachingTaskLesson schoolTeachingTaskLesson=new SchoolTeachingTaskLesson();
//        for (Serializable serializable:ids){
//            //查找要删除的对象是否为空
//            schoolTeachingTaskLesson=mapper.findById(serializable);
//            //判断课堂的教学任务是否存在
//            SchoolTeachingTask schoolTeachingTask=schoolTeachingTaskMapper.findById(schoolTeachingTaskLesson.getTaskId());
//            if(schoolTeachingTask!=null){
//                throw new MyWebException("删除失败:教学任务存在无法删除!");
//            }
//            //判断课堂的教师是否存在
//            SchoolTeacher schoolTeacher=schoolTeacherMapper.findById(schoolTeachingTaskLesson.getCreator());
//            if(schoolTeacher!=null){
//                throw new MyWebException("删除失败:教学教师存在无法删除!");
//            }
//        }

//        //删除教学任务的关联
//        SchoolTeachingTaskLesson schoolTeachingTaskLesson=

    }


    @Override
    protected void afterGet(SchoolTeachingTaskLesson schoolTeachingTaskLesson) {
        //添加教学任务关联属性
        if (schoolTeachingTaskLesson.getTaskId() != null) {
            //查找教学任务课堂所属教学任务
            SchoolTeachingTask schoolTeachingTask = schoolTeachingTaskMapper.findById(schoolTeachingTaskLesson.getTaskId());
            //赋值
            schoolTeachingTaskLesson.setSchoolTeachingTask(schoolTeachingTask);
        }
        //添加教师关联属性
        if (schoolTeachingTaskLesson.getCreator() != null) {
            //查找教学任务课堂所属教师
            SchoolTeacher schoolTeacher = schoolTeacherMapper.findById(schoolTeachingTaskLesson.getCreator());
            //赋值
            schoolTeachingTaskLesson.setSchoolTeacher(schoolTeacher);
        }
    }

    @Override
    protected void afterFindAll(List<SchoolTeachingTaskLesson> list) {
        for (SchoolTeachingTaskLesson schoolTeachingTaskLesson : list) {
            //添加教学任务关联属性
            if (schoolTeachingTaskLesson.getTaskId() != null) {
                //查找教学任务课堂所属教学任务
                SchoolTeachingTask schoolTeachingTask = schoolTeachingTaskMapper.findById(schoolTeachingTaskLesson.getTaskId());
                //赋值
                schoolTeachingTaskLesson.setSchoolTeachingTask(schoolTeachingTask);
            }

            //添加教师关联属性
            if (schoolTeachingTaskLesson.getCreator() != null) {
                //查找教学任务课堂所属教师
                SchoolTeacher schoolTeacher = schoolTeacherMapper.findById(schoolTeachingTaskLesson.getCreator());
                //赋值
                schoolTeachingTaskLesson.setSchoolTeacher(schoolTeacher );
            }
        }
    }


    @Override
    public List<SchoolTeachingTaskLesson> getStudentAllTechingTaskLesson() {
        String token= TokenUtil.getAppToken();//读取前台token的值;
        //获取登录账户信息
        SysUser user=sysUserTokenFrontendService.getUserFromToken(token);
        //获取当前登录的学生账户
        SchoolStudent schoolStudent=commonSchoolService.getStudent(user);
        //接收学生账户下的所有课堂
        List<SchoolTeachingTaskLesson> schoolTeachingTaskLessons=new ArrayList<>();
        List<SchoolTeachingTaskLesson> resultList=new ArrayList<>();
        //通过id查找是否存在教学任务学生中
        if(schoolStudent!=null){
            //通过教学任务学生表中的教学任务id查找教学任务
            List<SchoolTeachingTaskStudent> TeachingTaskStudentList=new ArrayList<>();
            TeachingTaskStudentList=  schoolTeachingTaskStudentMapper.findStudentbyid(schoolStudent.getId());
            //通过教学任务查找课堂
            //循环遍历取出教学任务学生对应的教学任务id
            for (SchoolTeachingTaskStudent schoolTeachingTaskStudent:TeachingTaskStudentList){
                //遍历通过教学任务id查找出的课堂
                schoolTeachingTaskLessons= mapper.findTaskLessonByTaskId(schoolTeachingTaskStudent.getTeachingTaskId());
                for (SchoolTeachingTaskLesson schoolTeachingTaskLesson:schoolTeachingTaskLessons){
                    resultList.add(schoolTeachingTaskLesson);
                }
            }

//            //加入教师外键
//            SchoolTeacher schoolTeacher=new SchoolTeacher();
//            for (SchoolTeachingTaskLesson schoolTeachingTaskLesson:resultList){
//                schoolTeacher= schoolTeacherMapper.findById(schoolTeachingTaskLesson.getCreator());
//                schoolTeachingTaskLesson.setSchoolTeacher(schoolTeacher);
//            }
        }
        return resultList;
    }

}
